<?php
namespace app\adm\controller;
use think\Controller;
use think\Db;
use think\Request;
class Activity extends Index
{
	public function __construct(Request $request){
		parent::__construct($request);
	}
	/**
	* 后台传记列表
	*/
	public function zhuanjilist(){
		$page = Db::name('zhuanji')->where(['zhuanjitype'=>'1'])->paginate(8,false,['type' => 'bootstrap','var_page' => 'page',]);
		$zhuanjilist = $page->all();
		foreach ($zhuanjilist as $key => $value) {
			$zhuanjilist[$key]['nickname']=Db::name('user')->where(['userid'=>$zhuanjilist[$key]['userid']])->value('nickname');
		}
		$this->assign('page',$page);
		$this->assign('zhuanjilist',$zhuanjilist);
		return $this->fetch('zhuanjilist',['title'=>'后台传记列表']);
	}
	/**
	* 发表后台传记
	*/
	public function zhuanjiadd(){
		// return view('zhuanjiadd');
		return $this->fetch('zhuanjiadd',['title'=>'后台传记添加']);
	}
	/*
	* 添加操作
	*/
	public function zhuanjiInsert()
	{
		if(request()->isPost()){
			if(input('post.zhuanjititle')=="" || input('post.zhuanjicontent')==''){
				jsBack('标题和内容不可以为空');
				return false;
			}
			//上传图片
			$file = request()->file('zhuanjiphoto');
			if($file){
			    // 移动到框架应用根目录/public/uploads/zhuanji 目录下
		        $info = $file->validate(['size'=>2*1024*1024,'ext'=>'jpg,png,gif'])->move(ROOT_PATH . 'public' . DS . 'uploads/zhuanji');
		        //上传成功后获取传记信息
		        if($info){
		        	$data['zhuanjiphoto']='/public/uploads/zhuanji/'.date("Ymd").'/'.$info->getFilename();
			        $data['zhuanjititle']=input('post.zhuanjititle');
					$data['zhuanjicontent']=input('post.zhuanjicontent');
					$data['addtime']=time();
					$data['userid']=session('userid');
					//后台公共传记类型为1
					$data['zhuanjitype']='1';
					$zhuanji=Db::name('zhuanji')->insert($data);
					//传记添加
					if($zhuanji){
						jsBack('添加成功','/adm/activity/zhuanjilist');
					}else{
						jsBack('网络有误,请重试');
					}
		        }else{//输出错误信息
		        	jsBack($file->getError());
		            return false;
		        }
		    }else{
		    	jsBack('请选择传记展示图');
		    }
		}else{
			//如果不是post跳转会添加页
			$this->redirect('activity/zhuanjiadd');
			// echo "111";
		}
	}
	//删除后台传记
	public function del_zhuanji(){
		$zhuanjiid = input('zhuanjiid');
		$res = Db::name('zhuanji')->delete($zhuanjiid);
		if($res){
			jsBack('删除成功');
		}else{
			jsBack('删除失败');
		}
	}
	/**
	* 后台传记修改
	*/
	public function zhuanjiedit()
	{
		$where['zhuanjiid']=input('zhuanjiid');
		$zhuanji = Db::name('zhuanji')->where(['zhuanjiid'=>input('zhuanjiid')])->find();
		$this->assign('zhuanji',$zhuanji);
		return $this->fetch('zhuanjiedit',['title'=>'后台传记修改']);
	}
	public function zhuanjiUpdate()
	{
		if(request()->isPost()){
			if(input('post.zhuanjicontent')==''){
				jsBack('内容不可以为空');
				return false;
			}
			//上传图片
			$file = request()->file('zhuanjiphoto');
			if($file){
				$info = $file->validate(['size'=>2*1024*1024,'ext'=>'jpg,png,gif'])->move(ROOT_PATH . 'public' . DS . 'uploads/zhuanji');
				if($info){
					$data['zhuanjiphoto']='/public/uploads/zhuanji/'.date("Ymd").'/'.$info->getFilename();
					$data['zhuanjicontent']=input('post.zhuanjicontent');
					$data['addtime']=time();
					$zhuanjiUpdate = Db::name('zhuanji')->where(['zhuanjiid'=>input('zhuanjiid')])->update($data);
					if($zhuanjiUpdate){
						jsBack('修改成功','/adm/activity/zhuanjilist');
					}else{
						jsBack('修改失败');
					}
				}else{
					jsBack($file->getError());
					return false;
				}
			}else{
				jsBack('请选择祭祀展示图');
			}
		}else{
			$this->redirect('activity/zhuanjiedit');
		}
	}
}